//
//  ByCellMessage.m
//  mijifen_ios
//
//  Created by mc on 2017/10/20.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellMessage.h"

@implementation ByCellMessage
{
    UIView          *_bgView;
    UIView          *_headerView;
    UILabel         *_pay_nameLabel;
    UILabel         *_pay_codeLabel;
    UILabel         *_create_timeLabel;
    UILabel         *_priceLabel;
    UILabel         *_infoLabel;
    UILabel         *_pointLabel;
    UIImageView     *_pointImageView;
    UIView          *_lineView;
    UILabel         *_redLabel;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellMessage";
    ByCellMessage *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}


- (void)createViews{
    _bgView = [[UIView alloc] init];
    _bgView.layer.cornerRadius = 4.0;
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    _headerView = [[UIView alloc] init];
    _headerView.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [_bgView addSubview:_headerView];
    
    _pay_nameLabel = [[UILabel alloc] init];
    _pay_nameLabel.font = FONT(16);
    _pay_nameLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_pay_nameLabel];
    
    _pay_codeLabel = [[UILabel alloc] init];
    _pay_codeLabel.font = FONT(14);
    [_bgView addSubview:_pay_codeLabel];
    
    _create_timeLabel = [[UILabel alloc] init];
    _create_timeLabel.font = FONT(14);
    _create_timeLabel.textAlignment = NSTextAlignmentRight;
    [_bgView addSubview:_create_timeLabel];
    
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.font = FONT(24);
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_priceLabel];
    
    _infoLabel = [[UILabel alloc] init];
    _infoLabel.font = FONT(14);
    _infoLabel.textAlignment= NSTextAlignmentCenter;
    [_bgView addSubview:_infoLabel];
    
    _pointLabel = [[UILabel alloc] init];
    _pointLabel.font = FONT(24);
    _pointLabel.textColor = BY_TEXT_COLOR;
    _pointLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_pointLabel];
    
    _pointImageView = [[UIImageView alloc] init];
    [_bgView addSubview:_pointImageView];
    
    _lineView = [[UIView alloc] init];
    [_bgView addSubview:_lineView];
    
    _redLabel = [[UILabel alloc] init];
    _redLabel.font = FONT(14);
    [_bgView addSubview:_redLabel];
    
}

- (void)setMessageModel:(ByModelMessage *)messageModel{
    _messageModel = messageModel;
    
    CGFloat width = SCREEN_WIDTH - 15 * 2;
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self).offset(15);
        make.size.mas_equalTo(CGSizeMake(width, 170 + 30 + 15 + 1 + 5 + 20 + 5));
    }];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView.mas_top);
        make.left.equalTo(_bgView.mas_left);
        make.size.mas_equalTo(CGSizeMake(width, 45));
    }];
    
    [_pay_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView).offset(12.5);
        make.left.equalTo(_bgView.mas_left);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    NSString *codeStr = [NSString stringWithFormat:@"交易单号：%@",_messageModel.pay_code];
    width = [codeStr getSizeOfString:14].width + 10;
    [_pay_codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.top.equalTo(_headerView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    width = SCREEN_WIDTH - 15 * 2 - 10 * 3;
    [_create_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_bgView.mas_right).offset(- 10);
        make.top.equalTo(_pay_codeLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    width = SCREEN_WIDTH - 15 * 2 - 10 * 2;
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_create_timeLabel.mas_bottom).offset(15);
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel.mas_left);
        make.top.equalTo(_priceLabel.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    
    width = [_messageModel.point getSizeOfString:24].width + 10;
    [_pointLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@((SCREEN_WIDTH - width - 15 * 2 - 10 * 2 - 30) / 2));
        make.top.equalTo(_infoLabel.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    
    [_pointImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_pointLabel.mas_right);
        make.top.equalTo(_pointLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    width = SCREEN_WIDTH - 15 * 2 - 10 * 2;
//    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_bgView.mas_left).offset(10);
//        make.top.equalTo(_pointLabel.mas_bottom).offset(15);
//        make.size.mas_equalTo(CGSizeMake(width, 1));
//    }];
    _lineView.frame = CGRectMake(10,215 , width, 1);
    [CALayer drawDashLine:_lineView lineLength:5 lineSpacing:5 lineColor:[UIColor lightGrayColor]];
//    [CALayer drawDashLine:_lineView lineFrame:CGRectMake(0, 0, width, 1) lineLength:5 lineSpacing:5 lineColor:[UIColor lightGrayColor]];
    
    [_redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lineView.mas_left);
        make.top.equalTo(_lineView.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    
    // 我的测试数据
    _pay_nameLabel.text = [NSString stringWithFormat:@"%@向您支付",_messageModel.pay_name];
    _pay_codeLabel.text = [NSString stringWithFormat:@"交易单号：%@",_messageModel.pay_code];
    _create_timeLabel.text = _messageModel.create_time;
    _priceLabel.text = _messageModel.price;
    _infoLabel.text = @"获得积分";
    _pointLabel.text = _messageModel.point;
    _pointImageView.image = [UIImage imageNamed:@"Message_point"];
    _redLabel.text = [NSString stringWithFormat:@"红包抵扣 -%@元",_messageModel.red_price];

}
@end
